<?php

namespace app\common\model;

use think\Cache;
use think\Model;
use app\admin\model\information\Inventory as appInventory;
/**
 * 通用物料数据模型
 */
class Inventory extends Model
{
    protected $type = [
        'fCoefficient' => 'quantity'
    ];
    protected $append = [
        'is_thumb'
    ];
    
    public function getIsThumbAttr($value, $data)
    {
        return true;
    }
    // 表名
    
    public static function getList(){
        $field = "ifnull(warehouse_id,'') warehouse_id,ifnull(shelves_id,'') shelves_id,
number,name,ifnull(specification,'') specification,ifnull(attributes,'') attributes,classification_name,
classification_id,unit,unit_code,mbatch,ifnull(safe_num,'') safe_num,
ifnull(max_num,'') max_num,ifnull(min_num,'') min_num";
        $data = self::field($field)->where([
            'deletetime' => null
        ])->select();
        return $data;
    }
    
    //获取最新仓库、库区、货架
    public static function getTjWhShByInvNumber($inv_number_id){
        if(!$inv_number_id){
            return ;
        }
        
        $inventory = db('inventory')->where('id',$inv_number_id)
        ->field('default_warehouse_id,shelves_id')
        ->find();

        $inv_shelves_latest = db('inv_shelves_latest_view')
        ->field('default_warehouse_id,warehouse_id,shelves_id,warehouse_name,shelves_name')
        ->where('inv_id',$inv_number_id)
        ->find();
        
        $res = [
            'warehouse_id' => $inventory['default_warehouse_id'],
            'shelves_id' => $inventory['shelves_id']
        ];

        if($inv_shelves_latest){
            $res = array_merge($res,[
                'lastest_warehouse_id' => $inv_shelves_latest['warehouse_id'],
                'lastest_shelves_id' => $inv_shelves_latest['shelves_id'],
                'lastest_warehouse_name' => $inv_shelves_latest['warehouse_name'],
                'lastest_shelves_name' => $inv_shelves_latest['shelves_name'],
            ]);
        }
        return $res;
    }

}
